package com.siriuscloud.holidayreservation.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.siriuscloud.holidayreservation.domain.AppointNumber;
import com.siriuscloud.holidayreservation.domain.Department;
import com.siriuscloud.holidayreservation.domain.Matter;
import com.siriuscloud.holidayreservation.mapper.AppointNumberMapper;
import com.siriuscloud.holidayreservation.mapper.DepartmentMapper;
import com.siriuscloud.holidayreservation.mapper.HolidayMapper;
import com.siriuscloud.holidayreservation.mapper.MatterMapper;
import com.siriuscloud.holidayreservation.utils.MyPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private HolidayMapper holidayMapper;

    @Resource
    private AppointNumberMapper appointNumberMapper;

    @Resource
    private MatterMapper matterMapper;

    // 获取所有部门
    public List<Department> getAll() {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        return departmentMapper.selectList(queryWrapper);
    }

    // 根据分页和name获取所有部门
    public Page<Department> getAllByPage(MyPage myPage, String name) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<Department>();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        Page<Department> page = new Page<>(myPage.getCurrent(), myPage.getPageSize(), true);
        return departmentMapper.selectPage(page, queryWrapper);
    }

    // 新增一个部门
    public Boolean create(Department department) throws Exception {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", department.getName());
        if (departmentMapper.selectCount(queryWrapper) > 0) {
            throw new Exception("部门名称不能重复");
        }
        if (departmentMapper.insert(department) == 0) {
            return false;
        }
        holidayMapper.insertByHoliday(department);
        return true;
    }

    // 编辑一个部门
    public Boolean update(Department department) {
        return departmentMapper.updateById(department) > 0;
    }

    // 删除部门
    public Boolean delete(String id) {
        QueryWrapper<AppointNumber> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_id", id);
        if (departmentMapper.deleteById(id) > 0) {
            appointNumberMapper.delete(queryWrapper);
            QueryWrapper<Matter> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("department_id", id);
            matterMapper.delete(queryWrapper1);
            return true;
        }
        return false;
    }

    public Department getDepartmentById(String id) {
        return departmentMapper.selectById(id);
    }

}
